<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            批量生成送检单
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 发货单号" id="consignment" value=""
                            onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">送检位置</div>
                    <select id="uda1">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">送检单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 送检单号" id="inspection" value=""
                            onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱号" id="tm" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描托号" id="trayyinfoid" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">操作类型 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="bind" name="operateType" checked>绑定</label> 
                        <label><input class="aui-radio" type="radio" value="del" name="operateType">解绑</label>
                        <label><input class="aui-radio" type="radio" value="th" name="operateType">退货</label>
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="createInspectionBill()" id="submitButton">生成送检单</div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="createUqcmChkBill()">创建检测单</div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="replaceBox()">替换箱码</div>
        <div style="width:100%;margin-top:2%;"></div>

        <div style="background-color:white; width: 100%;margin-top:2%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch;">

            <table class="table" style='width:750px;' id="table1">
                <tbody style="display:block" id="tbodyHtml">
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                      <td algin='center' style="width:50px;">序号</td>
                      <td algin='center' style="width:75px;">检测单</td>
                      <td algin='center' style="width:135px;">箱号</td>
                      <td algin='center' style="width:155px;">物料号</td>
                      <td algin='center' style="width:155px;">物料名</td>
                      <td algin='center' style="width:100px;">批次号</td>
                      <td algin='center' style="width:80px;">数量</td>
                    </tr>
                    <!-- <div id="tbodyHtml"> -->
                    <!-- <tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>
                        <td name='seqNo' style='width:50px;'>1</td>
                        <td name='jcd' style='width:75px;'>未创建</td>
                        <td name='boxnum' style='width:135px;'>B-231123-000003</td>
                        <td name='mrlCode' style='width:155px;'>1030400168</td>
                        <td name='mrlName' style='width:155px;'>红旗HS5牌照灯装饰板倒车摄像头及其支架逻辑总成(高配)-B</td>
                        <td name='lotCode' style='width:100px;'>2023112300001</td>
                        <td name='qty' style='width:80px;'>20</td>
                        </tr> -->
                    <!-- </div> -->
                </tbody>
            </table>
            <!-- <table class="table" style='width:750px;' id="table">
                <tbody id="tbodyHtml" style="display: block;height: 450px;overflow-y: scroll;">
                   <tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>
                        <td name='seqNo' style='width:50px;'>1</td>
                        <td name='jcd' style='width:75px;'>未创建</td>
                        <td name='boxnum' style='width:135px;'>B-231123-000003</td>
                        <td name='mrlCode' style='width:155px;'>1030400168</td>
                        <td name='mrlName' style='width:155px;'>红旗HS5牌照灯装饰板倒车摄像头及其支架逻辑总成(高配)-B</td>
                        <td name='lotCode' style='width:100px;'>2023112300001</td>
                        <td name='qty' style='width:80px;'>20</td>
                        </tr>
                </tbody>
            </table> -->
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var oldHtml ="<tr style='text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;'>"
                      +"<td algin='center' style='width:50px;'>序号</td>"
                      +"<td algin='center' style='width:75px;'>检测单</td>"
                      +"<td algin='center' style='width:135px;'>箱号</td>"
                      +"<td algin='center' style='width:155px;'>物料号</td>"
                      +"<td algin='center' style='width:155px;'>物料名</td>"
                      +"<td algin='center' style='width:100px;'>批次号</td>"
                      +"<td algin='center' style='width:80px;'>数量</td>"
                      +"</tr>";

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化查询仓库下拉框
    apiready = function () {

        $("#consignment").focus();
        idSelector = "consignment";
        selectUda1();
    }

    //查询待检区库位下拉框
    function selectUda1() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getDJworkCellList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "<option value=''>--请选择--</option>";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                }
                $api.html($api.byId("uda1"), html);
            }
        });

    }



    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            $("#" + idSelector).val(ret);

            //1：送检单号
            if (idSelector == 'inspection') {
                $("#inspection").blur();

                //查询送检单号数据
                searchBindBox();

                //3:条码
            } else if (idSelector == 'tm') {
                $("#tm").blur();

                tmSave();

                //发货单号
            } else if (idSelector == 'consignment') {
                $("#consignment").blur();

                //是否免检校验
                searchDetailsBeforCheck(ret);
            }else if (idSelector == 'trayyinfoid') {
                $("#trayyinfoid").blur();

                //托号绑定
                trayyinfoidBind();
}
        });
    }

    //托号绑定
    function trayyinfoidBind() {
        //取发货单号
        var consignment = $("#consignment").val();
        var inspection = $("#inspection").val();
        var trayyinfoid = $("#trayyinfoid").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            return;
        }

        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        ///tm = encodeURIComponent(encodeURIComponent(tm));

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/trayyinfoidBind";
        var data = {
            values: {
                consignment: consignment,
                inspection: inspection,
                trayyinfoid: trayyinfoid,
                un: un
            }
        };

        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                api.hideProgress();
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    api.toast({
                        msg: "托号绑定成功!",
                        duration: 3000
                    });

                    $("#tm").val("");

                    //刷新绑定数据
                    searchBindBox()

                    $("#tm").select();
                    idSelector = "tm";

                    //失败
                } else if (myobj.code == "1") {
                    $("#trayyinfoid").val("");
                    $("#trayyinfoid").select();
                    idSelector="trayyinfoid";

                   /*  $("#tm").select();
                    idSelector = "tm"; */
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }else{
                $("#trayyinfoid").val("");
                $("#trayyinfoid").select();
                 idSelector="trayyinfoid";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }


    //发货单监听回车事件
    $("#consignment").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') {
            if(consignment != null && consignment != "" && consignment != undefined){
                $("#consignment").blur();
            console.log(consignment);

            //发货单
            $("#consignment").val(consignment);
            searchDetailsBeforCheck(consignment);
            }
           
        }
    });

    //送检单监听回车事件
    $("#inspection").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var inspection = $(this).val();
        if (keycode == '13') {
            if(inspection != null && inspection != "" && inspection != undefined){
                $("#inspection").blur();

            //送检单
            $("#inspection").val(inspection);

            //查询送检单号数据
            searchBindBox();
            }
            
        }
    });

    //送检单监听回车事件
    $("#tm").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if(tm != null && tm != "" && tm != undefined){
                $("#tm").blur();
            console.log(tm);

            //送检单
            $("#tm").val(tm);
            tmSave();
            }
            
        }
    });
    //托号监听回车事件
    $("#trayyinfoid").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tray = $(this).val();
        if (keycode == '13') {
            if(tray != null && tray != "" && tray != undefined){
                $("#trayyinfoid").blur();
            console.log(tray);

            //送检单
            $("#trayyinfoid").val(tray);
            //托号绑定
            trayyinfoidBind();
            }
           
        }
    });


    //根据发货单查询表格数据
    function searchDetailsBeforCheck(consignment) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getConsignmentCheck";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                consignment: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    //失败
                } else if (myobj.code == "1") {

                    //$("#consignment").select();
                    //idSelector = "consignment";
                    $("#consignment").val("");
                    $("#consignment").select();
                    idSelector="consignment";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), oldHtml);
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }else{
                $("#consignment").val("");
                $("#consignment").select();
                 idSelector="consignment";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }

    //扫描条码后事件
    function tmSave() {
        if(!clickThrottle()) {
            return;
        }
        //取发货单号
        var consignment = $("#consignment").val();
        var inspection = $("#inspection").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {
            return;
        }

        if (inspection == "" || inspection == null || inspection == undefined) {
            return;
        }

        //操作类型
        var operateType = $("input[name='operateType']:checked").val();

        //绑定
        if (operateType == "bind") {

            bindBoxSave();

            //解绑
        } else if (operateType == "del") {

            delBoxSave();

            //退货
        } else if (operateType == "th") {

            thBoxSave();
        }
    }

    //箱码绑定保存
    function bindBoxSave() {
        //取发货单号
        var consignment = $("#consignment").val();
        var inspection = $("#inspection").val();
        var tm = $("#tm").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            return;
        }

        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        ///tm = encodeURIComponent(encodeURIComponent(tm));

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/bindInspectionBoxSave";
        var data = {
            values: {
                consignment: consignment,
                inspection: inspection,
                tm: tm,
                un: un
            }
        };

        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                api.hideProgress();
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    api.toast({
                        msg: "箱码绑定成功!",
                        duration: 3000
                    });

                    $("#tm").val("");

                    //刷新绑定数据
                    searchBindBox()

                    $("#tm").select();
                    idSelector = "tm";

                    //失败
                } else if (myobj.code == "1") {
                    /* $("#tm").select();
                    idSelector = "tm"; */
                    $("#tm").val("");
                    $("#tm").select();
                    idSelector="tm";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    //刷新绑定数据
                    searchBindBox()
                }
            }else{
                $("#tm").val("");
                $("#tm").select();
                 idSelector="tm";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }

    //箱码解除绑定保存
    function delBoxSave() {
        //取发货单号
        var consignment = $("#consignment").val();
        var inspection = $("#inspection").val();
        var tm = $("#tm").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            return;
        }

        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/delInspectionBoxSave";
        var data = {
            values: {
                consignment: consignment,
                inspection: inspection,
                tm: tm,
                un: un
            }
        };

        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                api.hideProgress();
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    api.toast({
                        msg: "箱码解绑成功!",
                        duration: 3000
                    });

                    $("#tm").val("");

                    //刷新绑定数据
                    searchBindBox()

                    $("#tm").select();
                    idSelector = "tm";

                    //失败
                } else if (myobj.code == "1") {
                    /* $("#tm").select();
                    idSelector = "tm"; */
                    $("#tm").val("");
                    $("#tm").select();
                    idSelector="tm";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    //刷新绑定数据
                    searchBindBox()
                }
            }else{
                $("#tm").val("");
                $("#tm").select();
                 idSelector="tm";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }

    //退货
    function thBoxSave() {
        //取发货单号
        // var consignment = $("#consignment").val();
        var inspection = $("#inspection").val();
        var tm = $("#tm").val();
        // //发货单为空，清空表格信息
        // if (consignment == "" || consignment == null || consignment == undefined) {
        //     api.toast({
        //         msg: "发货单号不能为空",
        //         duration: 3000
        //     });
        //     return;
        // }

        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/thInspectionBoxSave";
        var data = {
            values: {
                // consignment: consignment,
                inspection: inspection,
                tm: tm,
                un: un
            }
        };

        api.showProgress({
            title: '保存中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                api.hideProgress();
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    api.toast({
                        msg: "箱码退货成功!",
                        duration: 3000
                    });

                    $("#tm").val("");

                    //刷新绑定数据
                    searchBindBox()

                    $("#tm").select();
                    idSelector = "tm";

                    //失败
                } else if (myobj.code == "1") {
                    /* $("#tm").select();
                    idSelector = "tm"; */
                    $("#tm").val("");
                    $("#tm").select();
                    idSelector="tm";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    //刷新绑定数据
                    searchBindBox()
                }
            }else{
                $("#tm").val("");
                $("#tm").select();
                 idSelector="tm";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }

    //根据送检单查询表格数据
    function searchBindBox() {
        var inspection = $("#inspection").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchInspectionBindBoxData";
        var data = {
            values: {
                inspection: inspection,
                un: un
            }
        };
        api.showProgress({
            title: '查询中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                api.hideProgress();
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";

                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                        html += "<td name='seqNo' style='width:50px;'>" + (i+1) + "</td>";
                        html += "<td name='jcd' style='width:75px;'>" + list[i].jcd + "</td>";
                        html += "<td name='boxnum' style='width:135px;'>" + list[i].boxnum + "</td>";
                        html += "<td name='mrlCode' style='width:155px;'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName' style='width:155px;'>" + list[i].mrlName + "</td>";
                        html += "<td name='lotCode' style='width:100px;'>" + list[i].lotCode + "</td>";
                        html += "<td name='qty' style='width:80px;'>" + list[i].qty + "</td>";
                        html += "</tr>";
                    }

                    $("#tm").focus();
                    $("#tm").select();
                    idSelector = "tm";

                    
                    $api.html($api.byId("tbodyHtml"), oldHtml+" "+html);
                    $("#uda1").val(myobj.uda1);

                    //失败
                } else if (myobj.code == "1") {
                    $("#inspection").val("");
                    $("#inspection").select();
                    idSelector="inspection";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), oldHtml);
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }else{
                $("#inspection").val("");
                $("#inspection").select();
                 idSelector="inspection";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }

        });
    }

    //创建送检单
    function createInspectionBill() {

        //发货单号
        var consignment = $("#consignment").val();
        //送检位置
        var uda1 = $("#uda1").val();
        const submitButton = document.getElementById('submitButton');

        submitButton.style.pointerEvents = "none"
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空!", 
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }
        if (uda1 == "" || uda1 == null || uda1 == undefined) {
            api.toast({
                msg: "送检位置不能为空!",
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }
        api.showProgress({
            title: '生成中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/createInspectionBill";
        var data = {
            values: {
                consignment: consignment,
                uda1: uda1,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == "0") {
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    $("#inspection").val(myobj.inspection);
                    api.toast({
                        msg: "送检单创建成功",
                        duration: 3000
                    });

                    //刷新绑定数据
                    searchBindBox()

                    //失败
                } else if (myobj.code == "1") {
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    if(myobj.inspection != "" && myobj.inspection != null && myobj.inspection != undefined){
                        $("#inspection").val(myobj.inspection);
                    }
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }else{
                api.hideProgress();
                submitButton.style.pointerEvents = "auto"
            }
        });
    }

    //生成检测单
    function createUqcmChkBill() {

        //发货单号
        var consignment = $("#consignment").val();
        //送检单号
        var inspection = $("#inspection").val();

        //发货单号非空校验
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            return;
        }

        //送检单号非空校验
        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({ 
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '创建中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/createUqcmChkBillHake";
        var data = {
            values: {
                consignment: consignment, //发货单号
                inspection: inspection, //送检单号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    var urlDetails = serverUrl + "/app/createChkBillDetailsHaKe";
                    var data = {
                        values: {
                            consignment: consignment, //发货单号
                            inspection: inspection, //送检单号
                            un: un
                        }
                    };
                    api.ajax({
                        url: urlDetails,
                        method: 'post',
                        dataType: 'json',
                        timeout: 300,
                        returnAll: false,
                        data: data
                    }, function (ret, err) {
                        if (ret) {
                            var data = JSON.stringify(ret);
                            var result = eval("(" + data + ")");
                            //成功
                            if (result.code == "0") {

                                api.hideProgress();
                                api.toast({
                                    msg: result.message + "检测单创建成功！",
                                    duration: 3000
                                });

                                searchBindBox();

                            } else if (result.code == "1") {

                                api.hideProgress();
                                api.toast({
                                    msg: result.message,
                                    duration: 5000
                                });
                            }
                        } else {
                            api.hideProgress();
                        }
                    });

                    //失败,模板存在性校验
                } else if (myobj.code == "1") {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }

    //替换箱码
    function replaceBox() {
        //送检单号
        var inspection = $("#inspection").val();

        //送检单号非空校验
        if (inspection == "" || inspection == null || inspection == undefined) {
            api.toast({
                msg: "送检单号不能为空",
                duration: 3000
            });
            return;
        }

        api.openWin({
            name: 'uwmInspection_repllace',
            url: 'uwmInspection_repllace.html',
            pageParam: {
                inspection: inspection,
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
